package com.zeyuan.rpcclient.netty.pro.ProtocolDesign;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;

import java.nio.charset.StandardCharsets;

public class TestRedis {
    public static void main(String[] args)  {
        final byte[] LINE={13,10}; //换行和回车
        NioEventLoopGroup worker = new NioEventLoopGroup();
        //resp协议：set key value 在resp中： 先发数组长度3，和每个kv和命令的长度
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.group(worker);
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler());
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                    @Override //连接建立的时候发送给localhost:6379
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        ByteBuf buffer = ctx.alloc().buffer();
                        buffer.writeBytes("*3".getBytes(StandardCharsets.UTF_8)); //起始符
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("$3".getBytes(StandardCharsets.UTF_8)); //长度为3
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("set".getBytes(StandardCharsets.UTF_8));
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("$4".getBytes(StandardCharsets.UTF_8)); //长度为4
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("name".getBytes(StandardCharsets.UTF_8));
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("$8".getBytes(StandardCharsets.UTF_8)); //长度为8
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("zhangsan".getBytes(StandardCharsets.UTF_8));
                        buffer.writeBytes(LINE);
                        ctx.writeAndFlush(buffer);
                        super.channelActive(ctx);
                    }

                    @Override //redis发送过来消息，这边进行接收
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        ByteBuf buf= (ByteBuf) msg;
                        System.out.printf( buf.toString());
                        super.channelRead(ctx, msg);
                    }
                });
            }
        });
        try {
            ChannelFuture future = bootstrap.connect("49.235.198.193", 6379).sync();
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            worker.shutdownGracefully();
        }


    }
}
